<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>对多个数组或多维数组进行排序</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.array-merge.html">« array_merge</a></li>
      <li style="float: right;"><a href="function.array-pad.html">array_pad »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.array.html">数组 函数</a></li>
    <li>对多个数组或多维数组进行排序</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.array-multisort" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">array_multisort</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">array_multisort</span> &mdash; <span class="dc-title">对多个数组或多维数组进行排序</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.array-multisort-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>array_multisort</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">array</span> <code class="parameter reference">&$array1</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$array1_sort_order</code><span class="initializer"> = SORT_ASC</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$array1_sort_flags</code><span class="initializer"> = SORT_REGULAR</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">...$rest</code></span><br>): <span class="type">bool</span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>array_multisort()</strong></span>
   可以用来一次对多个数组进行排序，或者根据某一维或多维对多维数组进行排序。
  </p>
  <p class="para">
   关联（<span class="type">string</span>）键名保持不变，但数字键名会被重新索引。
  </p>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
 <p class="para">
  如果两个成员完全相同，那么它们将保持原来的顺序。
  在 PHP 8.0.0 之前，它们在排序数组中的相对顺序是未定义的。
 </p>
</p></blockquote>

 </div>

 
 <div class="refsect1 parameters" id="refsect1-function.array-multisort-parameters">
  <h3 class="title">参数</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
 <p class="para">
    重置数组中的内部指针，指向第一个元素。
 </p>
</p></blockquote>

  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">array1</code></dt>

     <dd>

      <p class="para">
       要排序的 <span class="type">array</span>。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">array1_sort_order</code></dt>

     <dd>

      <p class="para">
       之前 <span class="type">array</span>  参数要排列的顺序。
       <strong><code>SORT_ASC</code></strong> 按照上升顺序排序， <strong><code>SORT_DESC</code></strong> 按照下降顺序排序。
      </p>
      <p class="para">
       此参数可以和 <code class="parameter">array1_sort_flags</code> 互换，也可以完全删除，默认是 <strong><code>SORT_ASC</code></strong> 。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">array1_sort_flags</code></dt>

     <dd>

      <p class="para">
       为 <span class="type">array</span> 参数设定选项：
      </p>
      <p class="para">
       排序类型标志：
       <ul class="itemizedlist">
        <li class="listitem">
         <span class="simpara"><strong><code>SORT_REGULAR</code></strong> - 
          将项目按照通常方法比较（不修改类型）
         </span>
        </li>
        <li class="listitem">
         <span class="simpara"><strong><code>SORT_NUMERIC</code></strong> - 按照数字大小比较</span>
        </li>
        <li class="listitem">
         <span class="simpara"><strong><code>SORT_STRING</code></strong> -  按照字符串比较</span>
        </li>
        <li class="listitem">
         <span class="simpara">
          <strong><code>SORT_LOCALE_STRING</code></strong> -  根据当前的本地化设置，按照字符串比较。 
          它会使用 locale 信息，可以通过 <span class="function"><a href="function.setlocale.html" class="function">setlocale()</a></span> 修改此信息。
         </span>
        </li>
        <li class="listitem">
         <span class="simpara">
          <strong><code>SORT_NATURAL</code></strong> - 以字符串的&quot;自然排序&quot;，类似 <span class="function"><a href="function.natsort.html" class="function">natsort()</a></span>
         </span>
        </li>
        <li class="listitem">
         <span class="simpara">
          <strong><code>SORT_FLAG_CASE</code></strong> - 可以组合
          (按位或 OR) 
          <strong><code>SORT_STRING</code></strong> 或者
          <strong><code>SORT_NATURAL</code></strong> 大小写不敏感的方式排序字符串。
         </span>
        </li>
       </ul>
      </p>
      <p class="para">
       参数可以和 <code class="parameter">array1_sort_order</code> 交换或者省略，默认情况下是 <strong><code>SORT_REGULAR</code></strong>。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">rest</code></dt>

     <dd>

      <p class="para">
       可选的选项，可提供更多数组，跟随在 sort order 和 sort flag 之后。
       提供的数组和之前的数组要有相同数量的元素。
       换言之，排序是按字典顺序排列的。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.array-multisort-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>true</code></strong>， 或者在失败时返回 <strong><code>false</code></strong>。
  </p>
 </div>

 
 <div class="refsect1 examples" id="refsect1-function.array-multisort-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4937">
    <p><strong>示例 #1 多个数组排序</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$ar1&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">10</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">100</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">100</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$ar2&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">array_multisort</span><span style="color: #007700">(</span><span style="color: #0000BB">$ar1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$ar2</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$ar1</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$ar2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>
     这个例子里，排序后，第一个数组会包含  0、
     10、 100、 100。
     第二个数组会包含 4、1、 2、 3。
     第二个数组里的项目对应第一个数组后也进行了排序（100 和 100）。
    </p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(4) {
  [0]=&gt; int(0)
  [1]=&gt; int(10)
  [2]=&gt; int(100)
  [3]=&gt; int(100)
}
array(4) {
  [0]=&gt; int(4)
  [1]=&gt; int(1)
  [2]=&gt; int(2)
  [3]=&gt; int(3)
}
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4938">
    <p><strong>示例 #2 排序多维数组</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$ar&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">"10"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">11</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">100</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">100</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"a"</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array(&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"2"</span><span style="color: #007700">,&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br /></span><span style="color: #0000BB">array_multisort</span><span style="color: #007700">(</span><span style="color: #0000BB">$ar</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">],&nbsp;</span><span style="color: #0000BB">SORT_ASC</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SORT_STRING</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$ar</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">],&nbsp;</span><span style="color: #0000BB">SORT_NUMERIC</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SORT_DESC</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$ar</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>
     本例中在排序后，第一个数组将变成
     &quot;10&quot;，100，100，11，&quot;a&quot;（被当作字符串以升序排列）。第二个数组将包含
     1, 3, &quot;2&quot;, 2, 1（被当作数字以降序排列）。
    </p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(2) {
  [0]=&gt; array(5) {
    [0]=&gt; string(2) &quot;10&quot;
    [1]=&gt; int(100)
    [2]=&gt; int(100)
    [3]=&gt; int(11)
    [4]=&gt; string(1) &quot;a&quot;
  }
  [1]=&gt; array(5) {
    [0]=&gt; int(1)
    [1]=&gt; int(3)
    [2]=&gt; string(1) &quot;2&quot;
    [3]=&gt; int(2)
    [4]=&gt; int(1)
  }
}
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4939">
    <p><strong>示例 #3 对数据库结果进行排序</strong></p>
    <div class="example-contents"><p>
     本例中 <var class="varname">data</var>
     数组中的每个单元表示一个表中的一行。这是典型的数据库记录的数据集合。
    </p></div>
    <div class="example-contents"><p>
     例子中的数据如下：
    </p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
volume | edition
-------+--------
    67 |       2
    86 |       1
    85 |       6
    98 |       2
    86 |       6
    67 |       7
</pre></div>
    </div>
    <div class="example-contents"><p>
     数据全都存放在名为 <var class="varname">data</var>
     的数组中。这通常是通过循环从数据库取得的结果，例如
     <span class="function"><a href="function.mysql-fetch-assoc.html" class="function">mysql_fetch_assoc()</a></span>。
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$data</span><span style="color: #007700">[]&nbsp;=&nbsp;array(</span><span style="color: #DD0000">'volume'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">67</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'edition'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$data</span><span style="color: #007700">[]&nbsp;=&nbsp;array(</span><span style="color: #DD0000">'volume'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">86</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'edition'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$data</span><span style="color: #007700">[]&nbsp;=&nbsp;array(</span><span style="color: #DD0000">'volume'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">85</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'edition'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">6</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$data</span><span style="color: #007700">[]&nbsp;=&nbsp;array(</span><span style="color: #DD0000">'volume'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">98</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'edition'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$data</span><span style="color: #007700">[]&nbsp;=&nbsp;array(</span><span style="color: #DD0000">'volume'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">86</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'edition'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">6</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$data</span><span style="color: #007700">[]&nbsp;=&nbsp;array(</span><span style="color: #DD0000">'volume'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">67</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'edition'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">7</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>
     本例中将把 <var class="varname">volume</var> 降序排列，把
     <var class="varname">edition</var> 升序排列。
    </p></div>
    <div class="example-contents"><p>
     现在有了包含有行的数组，但是 <span class="function"><strong>array_multisort()</strong></span>
     需要一个包含列的数组，因此用以下代码来取得列，然后排序。
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;取得列的列表<br /></span><span style="color: #007700">foreach&nbsp;(</span><span style="color: #0000BB">$data&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$volume</span><span style="color: #007700">[</span><span style="color: #0000BB">$key</span><span style="color: #007700">]&nbsp;&nbsp;=&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'volume'</span><span style="color: #007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$edition</span><span style="color: #007700">[</span><span style="color: #0000BB">$key</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'edition'</span><span style="color: #007700">];<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;你可以使用&nbsp;array_column()&nbsp;代替上面的代码<br /></span><span style="color: #0000BB">$volume&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_column</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'volume'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$edition&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_column</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'edition'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;将数据根据&nbsp;volume&nbsp;降序排列，根据&nbsp;edition&nbsp;升序排列<br />//&nbsp;把&nbsp;$data&nbsp;作为最后一个参数，以通用键排序<br /></span><span style="color: #0000BB">array_multisort</span><span style="color: #007700">(</span><span style="color: #0000BB">$volume</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SORT_DESC</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$edition</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SORT_ASC</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$data</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>
     数据集合现在排好序了，结果如下：
    </p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
volume | edition
-------+--------
    98 |       2
    86 |       1
    86 |       6
    85 |       6
    67 |       2
    67 |       7
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4940">
    <p><strong>示例 #4 不区分大小写字母排序</strong></p>
    <div class="example-contents"><p>
     <strong><code>SORT_STRING</code></strong> 和
     <strong><code>SORT_REGULAR</code></strong>
     都是区分大小写字母的，大写字母会排在小写字母之前。
    </p></div>
    <div class="example-contents"><p>
     要进行不区分大小写的排序，就要按照原数组的小写字母拷贝来排序。
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$array&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'Alpha'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'atomic'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Beta'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'bank'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$array_lowercase&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #DD0000">'strtolower'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">array_multisort</span><span style="color: #007700">(</span><span style="color: #0000BB">$array_lowercase</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SORT_ASC</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SORT_STRING</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [0] =&gt; Alpha
    [1] =&gt; atomic
    [2] =&gt; bank
    [3] =&gt; Beta
)
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.array-multisort-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.usort.html" class="function" rel="rdfs-seeAlso">usort()</a> - 使用用户自定义的比较函数对数组中的值进行排序</span></li>
    <li class="member"><a href="array.sorting.html" class="link">数组排序函数对比</a></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>